package de.breitbeil.jbooking.dao;

import org.hibernate.event.PostDeleteEvent;
import org.hibernate.event.PostDeleteEventListener;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.richclient.application.event.LifecycleApplicationEvent;

/**
 * This class publishes an ApplicationEvent every time an object was deleted.
 * 
 * @author Dominik Breitbeil
 *
 */
public class DeleteListener implements PostDeleteEventListener, ApplicationContextAware {

    private ApplicationContext applicationContext;
    
    public void onPostDelete(PostDeleteEvent event) {
        Object deletedObject = event.getEntity();
        applicationContext.publishEvent(new LifecycleApplicationEvent(LifecycleApplicationEvent.DELETED, deletedObject));
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}
